/*
 * @lc app=leetcode.cn id=207 lang=cpp
 *
 * [207] 课程表
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    void canFinish(vector<set<int>> &pre, vector<bool>& flag_courses)
    {
        bool ret = false;
        do
        {
            ret = false;
            for (int i = 0; i < pre.size(); i++)
            {
                for(auto it = pre[i].begin(); it != pre[i].end(); )
                {
                    if(flag_courses[*it])
                    {
                        it = pre[i].erase(it);
                    }
                    else
                    {
                        it++;
                    }
                }
                if (!flag_courses[i] && pre[i].size() == 0)
                {
                    cout << i << endl;
                    ret = true;
                    flag_courses[i] = true;
                }
            }
        }while(ret);

        return;
    }
    bool canFinish(int numCourses, vector<vector<int>> &prerequisites)
    {
        vector<set<int>> pre(numCourses);
        vector<bool> flag_courses(numCourses, 0);

        for (const auto &v : prerequisites)
        {
            pre[v[0]].insert(v[1]);
        }
        canFinish(pre, flag_courses);
        // for(int i = 0; i < flag_courses.size() ; i++)
        // {
        //     if(!flag_courses[i]) return false;
        // }
        return accumulate(flag_courses.begin(), flag_courses.end(), 0) == numCourses;
        // return true;
    }
};
// @lc code=end
